var getIncidentUrl = 'http://api.traffy.in.th/apis/apitraffy.php?api=getIncident&key=AibgZsBJdV&appid=00001368&limit=20&format=JS&callback=addIncident';
//var getIncidentUrl = 'http://api.traffy.in.th/apis/apitraffy.php?api=getIncident&key=KVPukFGrZW&appid=00001395&limit=1&format=JS&call=addIncident';

var getVMSUrl = 'http://athena.traffy.in.th/apis/apitraffy.php?format=JS&api=getVMS&key=AibgZsBJdV&appid=00001368&call=addVMS&width=170&height=170';
//var getVMSUrl = 'http://athena.traffy.in.th/apis/apitraffy.php?format=JS&api=getVMS&key=hybrwGA8ht&appid=00001369&call=addVMS&width=170&height=170';
	
var getCCTVUrl = 'http://athena.traffy.in.th/apis/apitraffy.php?format=JS&api=getCCTV&key=AibgZsBJdV&appid=00001368&call=addCCTV';
//var getCCTVUrl = 'http://athena.traffy.in.th/apis/apitraffy.php?format=JS&api=getCCTV&key=hybrwGA8ht&appid=00001369&call=addCCTV';
	
function getIncidents() {
	var url = getIncidentUrl
	var script = document.createElement('script');
	script.setAttribute('src', url);
	document.getElementsByTagName('head')[0].appendChild(script);
}

function addIncident(t) {
	for ( var obj in t) { 
		for ( var inO in t[obj].news) {
			var desc = t[obj].news[inO].description;
			var latitude;
			var longitude;
			if(t[obj].news[inO].location.point!=null){
				latitude = t[obj].news[inO].location.point.latitude;
				longitude = t[obj].news[inO].location.point.longitude;
			}else{
				latitude = 0.0;
				longitude = 0.0;
			}
			var type = t[obj].news[inO].type;
			var starttime = t[obj].news[inO].starttime;
			var endtime = t[obj].news[inO].endtime;
			var title = t[obj].news[inO].title;

			var pointLatlng = new google.maps.LatLng(latitude, longitude);

			addMakerForIncident(type, starttime, endtime, desc, title,
					pointLatlng);
		}
	}
}

function addMakerForIncident(type, starttime, endtime, desc, title,
		pointLatlng) {
	var image = './resources/images/incident.png';
	var marker = new google.maps.Marker( {
		position :pointLatlng,
		map :map,
		title :type,
		icon :image
	});

	var contentString = '<div id="content">' + '<div id="siteNotice">'
			+ '</div>' 
			+ '<font size="0.2ex;">'
			//+'<b>Title: ' + title + '<br/>' + 'Type: '
			//+ type + '<br/>' 
			+ starttime 
			//+ ' To ' + endtime
			//+ '</b>'
			+'</font>' + '<div id="bodyContent"><textarea rows="5" cols="30">' + desc
			+ '</textarea></div>' + '</div>';
	var infowindow = new google.maps.InfoWindow( {
		content :contentString
	});

	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map, marker);
	});
}

function getVMS() {
	var url2 = getVMSUrl;
	var script2 = document.createElement('script');
	script2.setAttribute('src', url2);
	document.getElementsByTagName('head')[0].appendChild(script2);
}

function addVMS(t) {
	for ( var obj in t) {
		var latitude = t[obj].point.lat;
		var longitude = t[obj].point.lng;
		var name = t[obj].name;
		var name_th = t[obj].name_th;
		var url = t[obj].url;

		var pointLatlng = new google.maps.LatLng(latitude, longitude);
		addMakerForVCM(pointLatlng, name, url, name_th);
	}
}

function addMakerForVCM(pointLatlng, name, url, name_th) {
	var image = './resources/images/vms.png';
	var marker = new google.maps.Marker( {
		position :pointLatlng,
		map :map,
		title :name,
		icon :image
	});

	var contentString = '<div id="content">' + '<div id="siteNotice">'
			+ '</div style="text-align: center;">' 
			//+ '<b>' + name_th
			//+ ' - ' + name + '</b>' 
			+ '<div id="bodyContent"><img src='
			+ url + '>' + '</img></div>' + '</div>';
	var infowindow = new google.maps.InfoWindow( {
		content :contentString
	});

	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map, marker);
	});
}

function getCCTV() {
	var url2 = getCCTVUrl; 
	var script2 = document.createElement('script');
	script2.setAttribute('src', url2);
	document.getElementsByTagName('head')[0].appendChild(script2);
}

function addCCTV(t) {
	for ( var obj in t) {
		var latitude = t[obj].point.lat;
		var longitude = t[obj].point.lng;
		var name = t[obj].name;
		var name_th = t[obj].name_th;
		var url = t[obj].url;
		var last_update = t[obj].lastupdate;

		var pointLatlng = new google.maps.LatLng(latitude, longitude);
		addMakerForCCTV(pointLatlng, name, url, name_th, last_update);
		//break;
	}
}

function addMakerForCCTV(pointLatlng, name, url, name_th, last_update) {
	//var image = './resources/images/camera-icon.png';
	var image = new google.maps.MarkerImage('./resources/images/camera-icon.png',
		      // This marker is 20 pixels wide by 32 pixels tall.
		      new google.maps.Size(20, 20),
		      // The origin for this image is 0,0.
		      new google.maps.Point(0,0)
		      // The anchor for this image is the base of the flagpole at 0,32.
		     // new google.maps.Point(0, 32)
      );
	var marker3 = new google.maps.Marker( {
		position :pointLatlng,
		map :map,
		title :name,
		icon :image
	});

	var contentString = '<div id="content" nowrap = "true">' + '<div id="siteNotice">'
			+ '</div>' 
			//+ '<p><b>' + name_th
			//+ ' - ' + name + '<br/>'
			//+ ' Last Update: ' + last_update + '</b></p>'
			+ '<div id="bodyContent"><img width="200", height="170", src='
			+ url + '>' + '</img></div>' + '</div>';
			
	var infowindow = new google.maps.InfoWindow( {
		content :contentString
	});

	google.maps.event.addListener(marker3, 'click', function() {
		infowindow.open(map, marker3);
	});
}